﻿// 2023/3/8: 首个版本
// 2023/9/22: 使用PassParent给ShowDialog传参
// 2023/9/25: 改为直接输入Icon对象
// 2024/6/25: 支持API3
// 2024/12/3: 使用App.ShowOpenFileDialog

using System;
using System.IO;
using Eto.Forms;
using Eto.Drawing;
using ASEva;
using ASEva.Samples;
using ASEva.UIEto;

namespace CommonSource
{
    class OfflineMapPathDialog : DialogPanel
    {
        public OfflineMapPathDialog(String path, Icon icon = null)
        {
            MapPath = path;

            Icon = icon;
            Title = chinese ? "离线地图" : "Offline map";
            SetFixMode(310, 45, true);

            var mainLayout = this.SetContentAsRowLayout();
            mainLayout.AddLabel(chinese ? "路径:" : "Path:");
            var linkPath = mainLayout.AddLinkButton(chinese ? "(未配置)" : "(Not configured)", true);
            var buttonRemove = mainLayout.AddButton(chinese ? "移除" : "Remove", false, 80);

            if (path != null)
            {
                linkPath.Text = Path.GetFileName(MapPath);
            }

            linkPath.Click += async delegate
            {
                var selected = await App.ShowOpenFileDialog(this, chinese ? "请选择一个gmdb文件" : "Please select a gmdb file", false, File.Exists(MapPath) ? Path.GetDirectoryName(MapPath) : null, new()
                {
                    [chinese ? "离线地图文件 (*.gmdb)" : "Offline Map File (*.gmdb)"] = [".gmdb"],
                });
                if (selected != null)
                {
                    MapPath = selected[0];
                    linkPath.Text = Path.GetFileName(MapPath);
                }
            };

            buttonRemove.Click += delegate
            {
                MapPath = null;
                linkPath.Text = chinese ? "(未配置)" : "(Not configured)";
            };
        }

        public String MapPath { get; set; }

        private bool chinese = AgencyLocal.GetAppLanguage() == Language.Chinese;
    }
}